package com.wifeacountbook;

import java.util.Calendar;
import java.util.Date;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.wifeacountbook.provider.AcountKeeper;
import com.wifeacountbook.util.AcountKeeper.IncomeDetail;

public class IncomeDetailListActivity extends BaseListActivity {
	private static final int PAY_TIME_DIALOG_ID = 2;
	private  ListView mList ;
	private AlertDialog datePickDialog;
	private SimpleCursorAdapter adapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.e("Test",">>>IncomeDetailListActivity onCreate run");
		setContentView(R.layout.layout_toolbar);
		mList = (ListView) findViewById(R.id.list);
	}
	@Override
	protected void onResume() {
		super.onResume();
		Log.e("Test",">>>IncomeDetailListActivity onResume run");
		setMonLabel(new Date());
		initAdaptor();
		updateCursor(new Date());
	}
	
	private void initAdaptor() {
		adapter = new SimpleCursorAdapter(this,
				R.layout.listrecorditemview, null, new String[] {
						AcountKeeper.IncomeDetail.PAYTYPE,
						AcountKeeper.IncomeDetail.ACOUNTID,
						AcountKeeper.IncomeDetail.MONEY ,
						AcountKeeper.IncomeDetail.INCOMETIME,
						AcountKeeper.IncomeDetail._ID},
				new int[] { R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4,R.id.record_id });
		
		mList.setAdapter(adapter);
		
		mList.setOnItemClickListener(this);
	}
	
	private void updateCursor(Date d){
		Date date = new Date();
		int offYear = d.getYear() - date.getYear() ;
		int offMon = d.getMonth() -date.getMonth()  ;
		int offMonEnd = offMon+1;
		
		String soffYear = offYear >= 0?"+"+offYear:""+offYear;
		String soffMon = offMon >=0?"+"+offMon:""+offMon;
		String soffMonEnd = offMonEnd>=0?"+"+offMonEnd:""+offMonEnd;
		
		String startDate = "date('now','start of month','"+soffYear+" year','"+soffMon+" month')";
		String endDate = "date('now','start of month','"+soffYear+" year','"+soffMonEnd+" month','-1 day')";
		//String endDate = "date('now','start of month','+1 month','-1 day')";
		
		String selection = IncomeDetail.INCOMETIME+">= "+startDate+  " and " + IncomeDetail.INCOMETIME + "<= "+endDate;
		Cursor cursor = getContentResolver().query(
				AcountKeeper.IncomeDetail.CONTENT_URI, null, selection, null, null);

		Log.e("Test", "IncomeDetail num:" + cursor.getCount());
		startManagingCursor(cursor);
		adapter.changeCursor(cursor);
	}
	
	@Override
	public String getAddAction() {
		return AcountRecorder.ACTION_INCOME_RECORD_ADD;
	}

	@Override
	public Uri getUri() {
		return AcountKeeper.IncomeDetail.CONTENT_URI;
	}
	
	@Override
	public String getViewAction() {
		return AcountRecorder.ACTION_INCOME_RECORD_VIEW;
	}
	
	protected Dialog onCreateDialog(int id) {
		switch(id){
		case PAY_TIME_DIALOG_ID:
			View date_pick = this.getLayoutInflater().inflate(R.layout.date_pick, null, false);
			datePickDialog = new AlertDialog.Builder(this).setTitle(R.string.consume_time)
			.setPositiveButton(R.string.str_check, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(datePickDialog != null){
						DatePicker datepick = (DatePicker) datePickDialog.findViewById(R.id.datePicker1);
						int year =	datepick.getYear();
						int month = datepick.getMonth();
						int day = datepick.getDayOfMonth();
						Calendar c = Calendar.getInstance();
						c.set(year, month, day);
						setMonLabel(c.getTime());
						updateCursor(c.getTime());
						datePickDialog.dismiss();
					}
				}
			}).setNegativeButton(R.string.str_chancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(datePickDialog != null)
						datePickDialog.cancel();
				}
			}).setView(date_pick).create();
			return datePickDialog;
		default:
				break;
		}
		return null;
	}
}
